package com.gzs.learn.springcloud.service;

import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.Data;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class Pom2Gradle {
    public static final String gradleVersion = "%s=%s\n";
    public static final String gradleDependency = "api (\"%s:%s:%s\")\n";
    public static final String excludeFmt = "exclude group: '%s', module: '%s'\n";

    public static void main(String[] args) throws IOException {
        try (OutputStream out = new FileOutputStream(new File("/tmp/build.gradle"))) {
            try (InputStream in = new FileInputStream("/tmp/dependency.xml")) {
                JacksonXmlModule module = new JacksonXmlModule();
                XmlMapper mapper = new XmlMapper(module);
                PomSimpleModel pomSimpleModel = mapper.readValue(in, PomSimpleModel.class);
                outPutVersion(pomSimpleModel, out);
                outPutDependency(pomSimpleModel, out);
            }
        }
    }

    public static void outPutVersion(PomSimpleModel pomSimpleModel, OutputStream out) throws IOException {
        Map<String, String> versionMap = pomSimpleModel.getProperties();
        String extStart = "ext {";
        out.write(extStart.getBytes(StandardCharsets.UTF_8));
        for (Map.Entry<String, String> entry : versionMap.entrySet()) {
            String version = String.format(gradleVersion, entry.getKey(), entry.getValue());
            out.write(version.getBytes(StandardCharsets.UTF_8));
        }
        out.write("}".getBytes(StandardCharsets.UTF_8));
    }

    public static void outPutDependency(PomSimpleModel pomSimpleModel, OutputStream out) throws IOException {
        pomSimpleModel.getProperties();
        List<Dependency> dependencies = pomSimpleModel.getDependencies();
        out.write(" dependencies { constraints{".getBytes(StandardCharsets.UTF_8));
        for (Dependency dependency : dependencies) {
            String version = dependency.getVersion();
            String versionKey = version.substring(2, version.length() - 1);
            String apiDependency = String.format(gradleDependency, dependency.getGroupId(), dependency.getArtifactId(), "$" + versionKey);
            if (!CollectionUtils.isEmpty(dependency.getExclusions())) {
                apiDependency += "{\n";
                StringBuilder sb = new StringBuilder();
                for (Exclusion exclusion : dependency.getExclusions()) {
                    sb.append(String.format(excludeFmt, exclusion.getGroupId(), exclusion.getArtifactId()));
                }
                apiDependency += sb.toString();
                apiDependency += "}\n";
            }

            out.write(apiDependency.getBytes(StandardCharsets.UTF_8));
        }
        out.write("}}".getBytes(StandardCharsets.UTF_8));
    }
}

@Data
class PomSimpleModel {
    private Map<String, String> properties;
    private List<Dependency> dependencies;
}

@Data
class Dependency {
    private String groupId;
    private String artifactId;
    private String version;
    private List<Exclusion> exclusions;
    private String type;
    private String scope;
    private String classifier;
}

@Data
class Exclusion {
    private String groupId;
    private String artifactId;
}
